package com.yidu.finance.view;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yidu.finance.dao.FinanceDao;
import com.yidu.finance.dao.FinanceTypeDao;
import com.yidu.finance.dao.MemberDao;
import com.yidu.finance.model.Finance;
import com.yidu.finance.model.FinanceType;
import com.yidu.finance.model.Member;

/**
 * 理财Servlet
 */
@WebServlet("/FinanceServlet")
public class FinanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	FinanceDao dao = new FinanceDao();
       
	FinanceTypeDao typeDao = new FinanceTypeDao();
	
	MemberDao memDao = new MemberDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action = request.getParameter("action");
		if("showList".equals(action)){
			showList(request,response);
		}else if("showForm".equals(action)){
			showForm(request,response);
		}else if("addOrUpdate".equals(action)){
			addOrUpdate(request,response);
		}else if("delete".equals(action)){
			delete(request,response);
		}
	}
 
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String id = request.getParameter("id");
		Integer rows = dao.delete(Integer.valueOf(id)); 
		response.sendRedirect("FinanceServlet?action=showList");
		 
	}

	/**
	 * 增加的方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void addOrUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String financeID = request.getParameter("financeID");
		
		
		String typeId = request.getParameter("typeID");
		String financeAmount = request.getParameter("financeAmount");
		String financeRemarks = request.getParameter("financeRemarks");
		
		HttpSession session = request.getSession();
		Member member = (Member) session.getAttribute("member");
		Integer mid = member.getMemberID();//得到会员的ID
		
		Finance finance = new Finance();
		finance.setMemberId(mid);
		finance.setTypeID(Integer.valueOf(typeId));
		finance.setFinanceAmount(Double.valueOf(financeAmount));
		finance.setFinanceRemarks(financeRemarks);
		
		int rows = 0 ;
		//判断页面上的financeID是否为空,如果为空的话,就是增加,否则就是修改
		if(financeID!=null && !"".equals(financeID)){
			finance.setFinanceID(Integer.valueOf(financeID));
			rows = dao.update(finance);
			 
		}else{
			rows = dao.insert(finance);
			
			
			
			
			Integer memId = finance.getMemberId();//会员ID
			Member mem = memDao.selectID(memId);
			Integer balance = mem.getMemberBalance();//余额
			
			//如果是增加,那么需要将会员表的余额增加
			Integer tid = finance.getTypeID();
			FinanceType type = typeDao.selectID(tid);
			if(type.getTypeFlag()==1){ //收入
				balance = balance+finance.getFinanceAmount().intValue();
			}else{ //支出
				balance = balance-finance.getFinanceAmount().intValue();
			}
			mem.setMemberBalance(balance);//将最新的余额给会员
			memDao.update(mem);
			session.setAttribute("member", mem);//更新session
		}
		 
		if(rows>0){
			response.sendRedirect("FinanceServlet?action=showList");
		}else{
			request.setAttribute("msg", "保存失败!");
			request.getRequestDispatcher("pages/finance/form.jsp").forward(request, response);
		}
		
	}


	/**
	 * 显示表单页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void showForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		String id = request.getParameter("id");
		
		//是否有ID传过来,有ID传过来表示是修改
		if(id!=null && !"".equals(id)){
			Finance finance = dao.selectID(Integer.valueOf(id));
			request.setAttribute("finance", finance);
		}
		
		
		//查询类型,下拉框
		List<FinanceType> listType = typeDao.selectAll();
		request.setAttribute("listType", listType);
		System.out.println(listType.size()+"====================");
		
		request.getRequestDispatcher("pages/finance/form.jsp").forward(request, response);
		
	}

	/**
	 * 显示列表的页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Member member = (Member) session.getAttribute("member");
		Integer mid = member.getMemberID();//得到会员的ID
		System.out.println("当前登录的Id是:"+mid);
		
		String typeId = request.getParameter("typeId");
		
		List<Finance> list = dao.selectAll(mid,typeId);
		
		
		
		request.setAttribute("list", list);
		
		List<FinanceType> listType = typeDao.selectAll();
		request.setAttribute("listType", listType);
		
		
		request.getRequestDispatcher("pages/finance/list.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		doGet(request, response);
	}

}
